/*
 * Copyright (C) <2009> <fscops> <Rishi B Agrawal> (postrishi@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version->
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE->  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "../include/ohsm.h"
#include "ohsm_u.h"

/* OHSM allocation test */

unsigned char get_tier_dir(char *nam, unsigned int len)
{
	unsigned int i;

	if (allocpol->cnt_dirs == 0)
		return 0;

	for (i = 0; i < allocpol->cnt_dirs; i++) {
		if (!strncmp(nam, allocpol->dir_tier[i].dir_name, len))
			return allocpol->dir_tier[i].tier;
	}
	return 0;
}

/************************************************************
 *
 * get_tier_name2 () compares the type of created file
 * with the given allocation policy file
 *
 * returns the tier-id specified in the allocation policy
 * file else zero for default allocation
 *
 ***********************************************************/
unsigned char get_tier_name2(char *namex)
{
	int i;
	
	for (i = 0; i < allocpol->cnt_types; i++)
		if (!strcmp(namex, allocpol->type_tier[i].type))
			return allocpol->type_tier[i].tier;
	return 0;
}

/***********************************************************
 *
 * get_tier_name () extracts the extension out of the
 * name and call get_tier_name2
 *
 * returns the tier-id specified in the allocation policy
 * file else zero for default allocation
 *
 ***********************************************************/
unsigned char get_tier_name(char *nam, unsigned int len)
{
	unsigned int i;
	unsigned char temp;

	if (allocpol->cnt_types == 0)
		return 0;

	/* If it is not null terminated we will crash */
	//BUG_ON(nam[len] != '\0');
	for (i = len - 1; i >= 0; i--) {
		if (*(nam + i) == '.') {
			temp = get_tier_name2((char *) (nam + i + 1));
			return temp;
		}
	}
	return 0;
}

/************************************************************
 *
 * get_tier_uid () compares the current uid with the
 * given uid in allocation policy file
 *
 * returns the tier-id specified in the allocation policy
 * file else zero for default allocation
 *
 ***********************************************************/

unsigned char get_tier_uid(uid_t uidx)
{
	int i;
	if (allocpol->cnt_users == 0)
		return 0;

	for (i = 0; i < allocpol->cnt_users; i++)
		if (uidx == allocpol->uid_tier[i].uid)
			return allocpol->uid_tier[i].tier;
	return 0;
}

/***********************************************************
 *
 * get_tier_gid () compares the current gid with the
 * given gid in allocation policy file
 *
 * returns the tier-id specified in the allocation policy
 * file else zero for default allocation
 *
 ***********************************************************/
unsigned char get_tier_gid(gid_t gidx)
{
	int i;

	if (allocpol->cnt_groups == 0)
		return 0;

	for (i = 0; i < allocpol->cnt_groups; i++)
		if (gidx == allocpol->gid_tier[i].gid)
			return allocpol->gid_tier[i].tier;
	return 0;
}

/************************************************************
 *
 * ohsm_get_tier () provides the tier id for the allocation
 * file.
 *
 * returns the tier-id specified in the allocation policy
 * file else zero for default allocation.
 * (first according to type then uid then gid)
 *
 ***********************************************************/

unsigned char ohsm_get_tier(struct ohsm_inode_info *inode, char *path, struct ohsm_allocation_policy *allocpol, unsigned char dir_home_tid)
{
	char nam[256];
	unsigned char retval;

	if (dir_home_tid)
		return dir_home_tid; 

	if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
		return 0;
	
	printf("Trying to get the tier for %s\n", path);
	printf("The tier of directory is %u\n", dir_home_tid);
	retval = get_tier_dir(path, strlen(path));
	if (retval)
		return retval;

	retval = get_tier_name(path, strlen(path));
	if (retval)
		return retval;

	retval = get_tier_uid(inode->i_uid);
	if (retval)
		return retval;

	retval = get_tier_gid(inode->i_gid);
	if (retval)
		return retval;
	return 0;
}
